package com.ts.api.module.app.form.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.constant.BizEnum;
import com.ts.api.common.constant.app.AppBizCodeEnum;
import com.ts.api.common.constant.channel.ChannelVersionEnum;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.common.context.ChannelUserContext;
import com.ts.api.exp.BizException;
import com.ts.api.module.app.form.service.AppFormService;
import com.ts.api.module.channel.entity.cache.ChannelCacheUser;
import com.ts.api.module.channel.entity.po.ChannelUserFormPO;
import com.ts.api.module.channel.entity.vo.ReqChannelFormVO;
import com.ts.api.module.channel.entity.vo.RespChannelFormFieldValueVO;
import com.ts.api.module.channel.service.ChannelFormService;
import com.ts.api.module.channel.service.DoubleWriteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  表单接口
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/app/form")
public class AppFormController {

    @Autowired
    private AppFormService appFormService;
    @Autowired
    private ChannelFormService channelFormService;
    @Autowired
    private DoubleWriteUserService doubleWriteUserService;

    /**
     *  表单列表
     *
     *  @param  requestVO
     *  @return ResultVO
     */
    @GetMapping("/list")
    public ResultVO<Object> list(@RequestParamVo RequestVO requestVO) {
        String bizCode = requestVO.get("bizCode", String.class);
        String bizFlow = requestVO.get("bizFlow", String.class);
        Assert.isTrueThrows(ValidParam.required(bizCode, bizFlow), new BizException(BizEnum.EMPTY));

        AppBizCodeEnum appBizCodeEnum = AppBizCodeEnum.getByCodeAndFlow(bizCode, bizFlow);
        Assert.isNotNullThrows(appBizCodeEnum, new BizException(BizEnum.FORM_PARAM_ERROR));

        //  e时代
        if (ChannelVersionEnum.APP_V1.hasVersion()) {
            //  TODO 修改缓存
//            String key = StringUtils.createBuilder(bizCode).append("#").append(bizFlow).append("#").append(ChannelContext.getUnicode()).toString();
//            List<RespChannelFormFieldValueVO> listChannelFormFieldValueVO = ChannelCache.FRONT_FORM.get(key, () -> );
            List<RespChannelFormFieldValueVO> respChannelFormFieldValueVO = RespChannelFormFieldValueVO.convertVO(appFormService.listEsdForm(appBizCodeEnum));
            return ResultVO.suc(respChannelFormFieldValueVO);
        }
        return ResultVO.suc();
    }

    /**
     *  表单提交
     *
     *  @param  listVO
     *  @return ResultVO
     */
    @PostMapping("/insert")
    public ResultVO<Object> insert(@RequestBody(required = false) List<ReqChannelFormVO> listVO) {
        ChannelCacheUser channelCacheUserBO = ChannelUserContext.get();
        Long channelId = ChannelContext.getId();

        //  验证表单
        channelFormService.checkFormParam(listVO);

        //  插入用户表单信息
        List<ChannelUserFormPO> listUserForm = CollUtils.newArrayList();
        listVO.forEach(t -> {
            ChannelUserFormPO userFormPO = new ChannelUserFormPO();
            userFormPO.setUserId(channelCacheUserBO.getUserId());
            userFormPO.setChannelId(channelId);
            userFormPO.setFormFieldId(t.getFieldId());
            userFormPO.setFormFieldValueId(t.getFieldValueId());
            userFormPO.setCreateTime(DateUtils.currentTime());
            listUserForm.add(userFormPO);
        });

        //  插入表单
        doubleWriteUserService.removeByUserId(channelCacheUserBO.getUserId());
        doubleWriteUserService.batchInsertUserForm(listUserForm);
        return ResultVO.suc();
    }
}
